﻿<%@ Page Language="c#" ContentType="text/html" ResponseEncoding="utf-8" Async="true" MasterPageFile="~/administration/list_private.master" Inherits="ACM.cmsarea08.Administration.Core.gesUPanel" %>
<%@ MasterType VirtualPath="~/administration/list_private.master" %>
<asp:Content ID="PageBody" Runat="server" ContentPlaceHolderID="MasterBody">
<div class="dynform dfbody dfmainbody">
<h1><img src="../template/admimg/user_model_big.jpg" alt="User Forms Manager" />Gestione Pannello Utente</h1>
<p>
Attraverso questo pannello è possibile assegnare i tools sia in area pubblica che in area privata ai ruoli utente.
  <br />
nota per ciccio:<br />
bisogna creare tutta la logica di lettura dei dati salvati, di selezione ed assegnazione dei nuovi dati</p>
<style type="text/css">
	#roles { margin:0px; padding:0px; width:80%; border:1px solid #333; font-size:11px; }
	#roles thead { font-weight:bold; background:#333; color:#e2e3e0; }
	#roles tbody { background:#FFF; } 
	#roles tbody td { border-bottom:1px solid #333; }
	
	#alf { margin:0px; display:block; clear:both; padding:0px; }
	#alf li { display:inline; font-size:11px; padding-right:10px; cursor:pointer; font-weight:bold; list-style:none; }
	#userChoose { font-weight:normal; font-size:11px; border:1px solid #000; padding:3px; background:#FFF; width:95%; margin-bottom:10px;}
	#userChoose span { padding-right:10px; }
	#userChoose a { margin-right:10px; font-size:11px; padding:1px 2px; }
	#userChoose a:hover { background:#666; color:#FFF; padding:1px 2px;}
	#editcommands { margin:5px 5px; padding:0px; }
	#editcommands li { list-style:none; display:block; width:20px; height:20px; 
		border:1px solid #666; background:#ccc; font-size:12px; font-weight:bold; text-align:center;
		line-height:1.8; margin:3px; cursor:pointer;  }
	
	#assignedRoles { float:left; width:43%; border-right:1px solid #000; background:#FFF;height:400px; overflow:auto; }	
	#assignedRoles ul { margin:5px; padding:5px; font-size:11px;  }
	#assignedRoles li { list-style:none; margin:2px 5px; padding:2px; cursor:pointer; }
	#avaiableRoles { float:left; width:43%; background:#e2e3e0; height:400px;}	
	#toolsRoles { float:left; width:50px; background:#e2e3e0; height:400px;}	
	#assignedRoles h3,  #avaiableRoles h3 { display:block; margin:0px; margin-bottom:5px; padding:7px; background:#666; color:#FFF; font-size:13px; }
		
</style>

<div class="choose_role" id="roleChooser">
    Scegli un Ruolo: 
    <ul id="alf"></ul>
    <table id="roles"></table>
</div>

<div class="assign_role" id="roleActions">
<div style="clear:both;" id="userChoose">selected role - <a href="#" title="close">Chiudi</a></div>
<div id="assignedRoles">
<h3>Assigned Panel</h3>
<ul id="myitems">
	<li rel='test1'>pannello 1</li>
    <li rel='test2'>pannello 2</li>
</ul>
</div>
<div id="toolsRoles">
	<ul id="editcommands">
		<li id="addone">&lt;</li>
		<li id="remone">X</li>
		<li id="clearall">XX</li>
	</ul>
</div>
<div id="avaiableRoles">
<h3>Avaiable Panels</h3>
 <select id="avaiable">
 	<option value="">-- choose --</option>
 	<option value="D">Default</option>
    <option value="M">from Modules</option>
 </select><br/>
 <div id="ava_din">

 </div>
</div>


</div>

</div>

<script language="javascript">
window.addEvent('domready', function() {
	var currentSelect = "";
	var currentFilter = "";	
	var tableRole =$('roles');
	var filterRole = $('alf');
	var panelUser  = $('userChoose');
	var _roleAction = $('roleActions');
	var _roleChooser  = $('roleChooser');	
	var _myitems = $('myitems');	
	var itemSort = null;	
	var LcurrentItem = "";		
	var RcurrentItem = null;	
	//button:
	var _addone = $('addone');
	var _remone = $('remone');
	var _remall = $('clearall');
	var edited  = false;				
	
	function clearSelection() {
		$$('#myitems li').each(function(item) {
			if(!item.getProperty('selected')) return;
			item.setProperty('selected','false');
			item.setStyle('background','#fff');
		});
		
	}
	function existsSelection(quale) {
		var retorno = false;
		$$('#myitems li').each(function(item) {
			if(!item.getProperty('rel')) return false;
			if(item.getProperty('rel')==quale) retorno= true;
		});
		return retorno;
	}	
	function initButtons() {
		if(!_myitems) return;
		/*
		itemSort = new Sortables(_myitems);
		
		$$('#myitems li').each(function(item) {
			item.addEvent('click',function() {
				edited=true;
				clearSelection();
				LcurrentItem = {'testo':this.innerHTML,'value':this.getProperty('rel')};
				this.setProperty('selected','true');
				this.setStyle('background','#ccc');
			});
		
		});
		*/
	
		if(!_addone) return;
		_addone.addEvent('click', function() {
			if(RcurrentItem==null) return;
			if( existsSelection (RcurrentItem.value )==true) return;
			var elemento = new Element ('li');
			elemento.set('html',RcurrentItem.testo);
			elemento.setProperty('rel',RcurrentItem.value);
			elemento.addEvent('click',function() {
				clearSelection();
				LcurrentItem = {'testo':this.innerHTML,'value':this.getProperty('rel')};
				this.setProperty('selected','true');
				this.setStyle('background','#ccc');
			});			
			_myitems.adopt(elemento);
			itemSort.addItems(elemento);
			edited=true;
			RcurrentItem = null;
		});
		if(!_remone) return;
		_remone.addEvent('click', function() {
			if(LcurrentItem==null) return;
			$$('#myitems li').each(function(item) {		
				item.setProperty('selected','false');
				item.setStyle('background','#fff');
				edited=true;
				if(item.getProperty('rel')==LcurrentItem.value) {
					itemSort.removeItems(item).destroy();
				}
			});			
			LcurrentItem = null;
		});
		if(!_remall) return;		
		_remall.addEvent('click', function() {
			edited=true;
			$$('#myitems li').each(function(item) {		
					itemSort.removeItems(item).destroy();
			});	
			LcurrentItem = null;
			RcurrentItem = null;
		});
	
	}
	
	
	function initFilter() {
		if(!filterRole) return;
		filterRole.empty();
		for(var i = 65; i<91;i+=3) {
			var endN = i+3;
			if(endN>90) endN = 90;
			var li = new Element('li');
			li.set('html',(String.fromCharCode(i) + '..' + String.fromCharCode(endN) ) );
			li.setProperty('rel',i+','+endN);
			li.addEvent('click',function() {
				//passa filtro:
				if(this.getProperty('rel')!=currentFilter) {
					currentFilter = this.getProperty('rel');
					initTable();
				}
			
			});
			filterRole.adopt(li);
		}
		var liA = new Element('li');
		liA.set('html','all');
		liA.setProperty('rel','all');
		liA.addEvent('click',function() {
			//passa filtro:
			if(this.getProperty('rel')!=currentFilter) {
				currentFilter = this.getProperty('rel');
				initTable();
			}
		
		});
		filterRole.adopt(liA);
		
		
	}

	function initTable() {
		if(!tableRole) return;
		tableRole.empty();	
		//aggiungo il head e body.
		var myH = new Element('thead');
		var td0 = new Element('td');
		var td1 = new Element('td');		
		var tr = new Element('tr');		
		td0.set('html','role name');
		td1.set('html','role dependances');		
		tr.adopt(td0);
		tr.adopt(td1);			
		myH.adopt(tr);
		tableRole.adopt(myH);
		loadTable();
	}
	function loadTable() {
		if(!tableRole) return;
		var filtroC= currentFilter;
		var querystring = "filt=all";
		if(filtroC) {
			if(filtroC!="") {
				querystring="filt=" + filtroC;
			} 
		}
		var r = new Request( {
			method: 'get', 
			url: '/administration/core/xml_server.asmx/getRolesList',
			onSuccess: function(text,xml) {
				if(!text) return;
				if(!xml) return;
				var cXml;
				if (window.ActiveXObject)
				{
					var xmlObj=new ActiveXObject("Microsoft.XMLDOM");
					xmlObj.async="false";
					xmlObj.loadXML(text);
					if(xmlObj.documentElement == null) {	alert('sono nullo'); return; }
					cXml = xmlObj.documentElement;		
				} else {
					cXml = xml.documentElement;
				}						
				if(cXml == null) {
					alert('unable load content.');
					return;
				}
				var sons = cXml.getElementsByTagName('Table');	
				var tBody = new Element('tbody');
				for(var i = 0 ; i<sons.length; i++ ) {
					var ruolo = "";
					var dipo  = "";
					for(var s = 0; s<sons[i].childNodes.length;s++) {
						if (sons[i].childNodes[s].nodeType != 1) continue;
						if(!sons[i].childNodes[s].firstChild) continue;
						if( sons[i].childNodes[s].nodeName == 'ruolo')
							ruolo = sons[i].childNodes[s].firstChild.nodeValue;
						if( sons[i].childNodes[s].nodeName == 'dipendenze')
							dipo = sons[i].childNodes[s].firstChild.nodeValue;
					}
					var tr = new Element('tr');
					var td0 = new Element('td');					
					var td1 = new Element('td');										
					td0.set('html',ruolo);
					td0.setProperty('rel',ruolo);					
					td0.addEvent('click', function() {
						currentSelect = this.getProperty('rel');
						loadSelection();
					});
					td1.set('html',dipo);					
					tr.adopt(td0);
					tr.adopt(td1);					
					tBody.adopt(tr);					
				}	
				tableRole.adopt(tBody);			
			}
		
		}).send(querystring);	
	
	}
	function loadSelection() {
		if(currentSelect=="") return;
		if(!panelUser) return;
		openAction();
		closeChooser();
		panelUser.empty();
		//load Data
		var cRole = new Element('span');
		var cClose = new Element('a');	
		var cSave = new Element('a');		
		cRole.set('html','Selected Role: <b>'+currentSelect+'</b>');
		cClose.set('html','close');		
		cSave.set('html','save');				
		cClose.addEvent('click',function() {
			var continues =false;
			if(edited) {
				continues = confirm('Tha content are changed, if you proceed any changes will be lost.');
			} else
				continues = true;
			if(!continues) return;
			currentSelect="";
			clearSelection();			
			RcurrentItem =null;
			LcurrentItem =null;				
			openChooser();
			closeAction();
			
		});
		cSave.addEvent('click',function() {
			clearSelection();			
			RcurrentItem =null;
			LcurrentItem =null;			
			var elementi = null;
			if(!itemSort) return;
			elementi = itemSort.serialize(0, function(element, index){
							var xme = '';
							if(element.getProperty('enabled') ) {
								if(element.getProperty('enabled')=='1') 
									xme = element.getProperty('rel') + "[" + element.innerHTML;
							} else
								xme = element.getProperty('rel') + "[" + element.innerHTML;
							return xme;
						}).join('#');
									
			edited=false;
			alert(elementi);
			//send request:
			var r = new Request( {
				method: 'get', 
				url: '/administration/core/xml_server.asmx/setUserPanel',
				onSuccess: function(text,xml) {
					alert(text);
					alert(xml);
				}
			}).send("panelList="+escape(elementi)+"&roleName="+currentSelect);			
			
			
		});		
		panelUser.adopt(cRole);
		panelUser.adopt(cSave);			
		panelUser.adopt(cClose);		
		//blocca i pannelli ed aggiungi un controllo.
		if(!_myitems) return;
		_myitems.empty();
		itemSort = new Sortables(_myitems);
		//effettua la richiesta:
		var r2 = new Request( {
				method: 'get', 
				url: '/administration/core/xml_server.asmx/getUserPanel',
				onSuccess: function(text,xml) {
					if(!text) return;
					if(!xml) return;
					var cXml;
					if (window.ActiveXObject)
					{
						var xmlObj=new ActiveXObject("Microsoft.XMLDOM");
						xmlObj.async="false";
						xmlObj.loadXML(text);
						if(xmlObj.documentElement == null) {	alert('sono nullo'); return; }
						cXml = xmlObj.documentElement;		
					} else {
						cXml = xml.documentElement;
					}						
					if(cXml == null) {
						alert('unable load content.');
						return;
					}
					
					var sons = cXml.getElementsByTagName('Table');	
					for(var i = 0 ; i<sons.length; i++ ) {
						var testo = "";
						var valore  = "";
						var ena  = "";
						for(var s = 0; s<sons[i].childNodes.length;s++) {
							if (sons[i].childNodes[s].nodeType != 1) continue;
							if(!sons[i].childNodes[s].firstChild) continue;
							if( sons[i].childNodes[s].nodeName == 'shortname')
								testo = sons[i].childNodes[s].firstChild.nodeValue;
							if( sons[i].childNodes[s].nodeName == 'valore')
								valore = sons[i].childNodes[s].firstChild.nodeValue;
							if( sons[i].childNodes[s].nodeName == 'enabled')
								ena = sons[i].childNodes[s].firstChild.nodeValue;
								
						}
						var opT = new Element('li');
						opT.set('html',testo);
						if(ena=='0') 
							opT.setStyle('color','#FF0000');
						opT.setProperty('rel',valore);
						opT.setProperty('id',valore);						
						opT.setProperty('enabled',ena);
						
						_myitems.adopt(opT);	
						if(ena!='0') {
							itemSort.addItems(opT);				
							opT.addEvent('click',function() {
								edited=true;
								clearSelection();
								LcurrentItem = {'testo':this.innerHTML,'value':this.getProperty('rel')};
								this.setProperty('selected','true');
								this.setStyle('background','#ccc');
							});							
							
						}
					}						



				}
			}).send("roleName="+currentSelect);			
	}
	function openAction() {
		if(!_roleAction) return;
		_roleAction.setStyles({
			'display':'block',
			'opacity':0
			});
		_roleAction.tween('opacity',1);
	}
	function openChooser() {
		if(!_roleChooser) return;
		_roleChooser.setStyles({
			'display':'block',
			'opacity':0
			});
		_roleChooser.tween('opacity',1);
	}	
	function closeChooser() {
		if(!_roleChooser) return;
		var fxA = new Fx.Tween(_roleChooser, {property:'opacity',link:'chain',duration:'short'} );
			fxA.start(1, 0).chain(
				function() {											   
					_roleChooser.setStyle('display','none');
				}
			);
	}
	function closeAction() {
		
		if(!_roleAction) return;
		var fxA = new Fx.Tween(_roleAction, {property:'opacity',link:'chain',duration:'short'} );
			fxA.start(1, 0).chain(
				function() {											   
					_roleAction.setStyle('display','none');
				}
			);
		
	}
	function initPanel() {
		if(!_roleAction) return;
		if(!_roleChooser) return;		
		_roleAction.setStyle('display','none');
		_roleChooser.setStyle('display','block');
	}
	
	function initSelector() {
		var s = $('avaiable');
		if(!s) return;
		s.addEvent('change', function() {
			//selezionato:
			alert(this.value);
			if(this.value=='D')
				loadDefault();
			else if(this.value=='M')
				loadModules();
			else
				unloadSelect();
		});
	}
	function loadDefault() {
		var des = $('ava_din');
		if(!des) return;
		des.empty();
		var lista = new Element('select');
		lista.setProperty('size','10');
		lista.set('html','<option value="D_profile">Profilo</option><option value="D_changepw">CambioPassword</option>');
		lista.addEvent('change',function() {
			var selected = '';
			var obj = this.getElements('option');
			obj.each(function(item) {
				if(item.getProperty('selected')==true)
					selected = item.innerHTML;
			});
		
			RcurrentItem = {'testo':selected,'value':this.value};
		});
		des.adopt(lista);		
	}
	function loadModules() {
		var des = $('ava_din');
		if(!des) return;	
		des.empty();
		// getModuleList
		var r = new Request( {
			method: 'get', 
			url: '/administration/core/xml_server.asmx/getModuleList',
			onSuccess: function(text,xml) {
				if(!text) return;
				if(!xml) return;
				var cXml;
				if (window.ActiveXObject)
				{
					var xmlObj=new ActiveXObject("Microsoft.XMLDOM");
					xmlObj.async="false";
					xmlObj.loadXML(text);
					if(xmlObj.documentElement == null) {	alert('sono nullo'); return; }
					cXml = xmlObj.documentElement;		
				} else {
					cXml = xml.documentElement;
				}						
				if(cXml == null) {
					alert('unable load content.');
					return;
				}
				var seletto = new Element('select');
				var sons = cXml.getElementsByTagName('Table');	
				var opTE = new Element('option');
				opTE.set('html','-- choose --');
				opTE.set('value','');
				seletto.adopt(opTE);					
				for(var i = 0 ; i<sons.length; i++ ) {
					var ruolo = "";
					var dipo  = "";
					for(var s = 0; s<sons[i].childNodes.length;s++) {
						if (sons[i].childNodes[s].nodeType != 1) continue;
						if(!sons[i].childNodes[s].firstChild) continue;
						if( sons[i].childNodes[s].nodeName == 'id')
							ruolo = sons[i].childNodes[s].firstChild.nodeValue;
						if( sons[i].childNodes[s].nodeName == 'titolo')
							dipo = sons[i].childNodes[s].firstChild.nodeValue;
					}
					var opT = new Element('option');
					opT.set('html',dipo);
					opT.set('value',ruolo);
					seletto.adopt(opT);					
				}	
				des.adopt(seletto);		
				
				seletto.addEvent('change',function() {
					var lchoose = $('mychoose');
					var existo = true;
					if(this.value=='') {
						RcurrentItem=null;
						if(lchoose) lchoose.dispose();
						return;					
					} 
					if(!lchoose) {
						lchoose = new Element('select');
						lchoose.setProperty('size','10');
						existo=false;
					}
					lchoose.empty();
					lchoose.setProperty('id','mychoose');
					if(!existo) des.adopt(lchoose);
					RcurrentItem=null;
					//altra richiesta:
					for(var i = 0 ; i<sons.length; i++ ) {
					var id = "";
					var adm  = "";
					var pub  = "";					
					for(var s = 0; s<sons[i].childNodes.length;s++) {
						if (sons[i].childNodes[s].nodeType != 1) continue;
						if(!sons[i].childNodes[s].firstChild) continue;
						if( sons[i].childNodes[s].nodeName == 'id')
							id = sons[i].childNodes[s].firstChild.nodeValue;
						if( sons[i].childNodes[s].nodeName == 'admin_tool')
							adm = sons[i].childNodes[s].firstChild.nodeValue;
						if( sons[i].childNodes[s].nodeName == 'public_tool')
							pub = sons[i].childNodes[s].firstChild.nodeValue;
							
					}
					if(id!=this.value) continue;
					if(adm!="") {
						var elm = new Element('option');
						elm.set('html','Administrative Panel');
						elm.set('value',"AM_"+this.value);
						lchoose.adopt(elm);					
					}
					if(pub!="") {
						var elm = new Element('option');
						elm.set('html','Public Panel');
						elm.set('value',"PM_"+this.value);
						lchoose.adopt(elm);					
					}
				}						
					lchoose.addEvent('change', function() {
						var selected = '';
						var obj = this.getElements('option');
						obj.each(function(item) {
							if(item.getProperty('selected')==true)
								selected = item.innerHTML;
						});
					
						RcurrentItem = {'testo':selected,'value':this.value};
					});
				
				});	
			}
		
		}).send();			
		
			
	}
	function unloadSelect() {
		RcurrentItem='';
		var des = $('ava_din');
		if(!des) return;
		des.empty();
	}	
	initPanel();
	initTable();
	initFilter();
	initSelector();
	initButtons();
});
</script>
</asp:content>